﻿/* To Do Section
 * TODO: Auto Server Reload (Done and seems to work. Watch it still though. Don't close before it works for some time)
 * TODO: Must work differently now, since there is no Webcontrol any more
 * 
 * TODO: LocalApplicationOptions: Insert Newline
 * 
 * TODO: ActionLogger Class with FIFO Buffer of configurable size. ?Start with 500 entries?
 * 
 * TODO: UpdateSeed.php Auswertung.
 */




using System;
using System.Collections.Generic;
//using CSharp.Libs.Koc.KernelViewModels;
//using CSharp.Libs.Koc.KernelModels.ItemClasses;
using CSharp.Libs.ExceptionHandling;
using CSharp.Libs.Koc;
using CSharp.Libs.Misc;
using CSharp.Libs.Koc.KernelModels;
using System.Collections;



namespace CSharp.Libs.Koc
{
    /// <summary>Kernel
    /// holds all needed classes for basic functionaltiy.</summary>
    /// <remarks>This is Version 2 of the Kernel class. It suites the new FiddlerCore support.
    /// It is possible to watch multiple servers at once with it.</remarks>
    /// <history>created  16.10.2011</history>
    public partial class Kernel
    {
        #region declarations
        private ExceptionCollector _EC = null;
        private LocalApplicationOptions _LAO = null;
        private TrafficMonitor _TM = null;
        private UserValidator _UV = null;
        private URLHolder _UH = null;
        //private DatabaseOperations _DBOps = null; //removed TOODOO: Put in own database layer
        private KocReloader _AKR = null;
        //private BaseSeedViewModel _BSVM = null;//removed TOODOO: Put in own ViewModel layer
        //private InfoViewModel _IVM = null;//removed TOODOO: Put in own ViewModel layer
        //private SoftwareAuthorizedViewModel _SAVM = null;//removed TOODOO: Put in own ViewModel layer
        private SeedCollector _SC = null;
        private MessageLogging _ML = null;

        //private Dictionary<Int32, MarchItem> _dictMarches = null;//removed TOODOO: Put in own database layer

        private Int32 _intInternalUID = 1;
        #endregion



        #region constructor / terminator
        public Kernel()
        {
            _EC = new ExceptionCollector();
            _LAO = new LocalApplicationOptions();

            _CheckOptionsFile();

            _TM = new TrafficMonitor(this);
            _UV = new UserValidator(this);
            _UH = new URLHolder(_EC);
            _AKR = new KocReloader(this);
            _SC = new SeedCollector(this);
            _ML = new MessageLogging();
        }

        ~Kernel()
        {
            _ML.Dispose();
            _ML = null;
            _SC = null;
            _AKR = null;
            _UH = null;
            _UV = null;
            _TM.Dispose();
            _TM = null;
            _LAO = null;
            _EC.Dispose();
            _EC = null;
        }
        #endregion



        #region private functions
        private void _CheckOptionsFile()
        {
            try
            {
                if (_LAO.OptionsCount == 0)
                {
                    _CreateDefaultOptions();
                }
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_CreateDefaultOptions
        /// 
        /// </summary>
        /// <history>29.04.2011 added custom servers</history>
        private void _CreateDefaultOptions()
        {
            try
            {
                //disable event
                _LAO.DoThrowOptionsChangedEvent = false;

                _LAO.SetComment("Working Facebook URL including http");
                _LAO.SetOption(constants.LAO_FACEBOOK_URL, "http://www.facebook.de");
                _LAO.SetComment("Main Source php Command");
                //_LAO.SetOption(constants.LAO_RECOGNIZE_STRING_PHP_MAIN_SRC, "main_src.php?");//28.01.2012 obsolete
                _LAO.SetOption(constants.LAO_SEEK_STRING_MAIN_SRC_SEED_START, "var seed");
                _LAO.SetOption(constants.LAO_SEEK_STRING_MAIN_SRC_SEED_END, ";var ");
                _LAO.SetOption(constants.LAO_RECOGNIZE_STRING_FB_SIG, "fb_sig");
                _LAO.SetOption(constants.LAO_KOC_RELOAD_URL, "http://apps.facebook.com/kingdomsofcamelot/?s=");
                _LAO.SetOption(constants.LAO_AUTOKOC_DB_FILE_NAME, "AutoKoc.s3db");
                _LAO.SetOption(constants.LAO_DEFAULT_SERVER_ID, "1");
                _LAO.SetOption(constants.LAO_KOC_RELOAD_TIMEOUT, "30");
                _LAO.SetOption(constants.LAO_KOC_CUSTOM_SERVER_1, "0");
                _LAO.SetOption(constants.LAO_KOC_CUSTOM_SERVER_2, "0");
                _LAO.SetOption(constants.LAO_KOC_CUSTOM_SERVER_3, "0");
                _LAO.SetOption(constants.LAO_KOC_CUSTOM_SERVER_4, "0");
                _LAO.SetOption(constants.LAO_KOC_CUSTOM_SERVER_5, "0");
                _LAO.SetOption(constants.LAO_TRAFFIC_MONITOR_DECRYPT_SSL, "false");

                //reanable event
                _LAO.DoThrowOptionsChangedEvent = true;
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_DataIsValid
        ///     returns true if all data is valid
        /// </summary>
        /// <returns></returns>
        private Boolean _DataIsValid()
        {
            try
            {
                return _UH.URLGetChat.URLMaybeValid && _TM.UserIsValid && _TM.Seeds.Count > 0;
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }

            return false;
        }


        #endregion



        #region interface
        #region properties
        #region kernel modules
        public ExceptionCollector EC
        {
            get
            {
                return _EC;
            }
        }

        public LocalApplicationOptions LAO
        {
            get
            {
                return _LAO;
            }
        }

        public TrafficMonitor TM
        {
            get
            {
                return _TM;
            }
        }

        public UserValidator UV
        {
            get
            {
                return _UV;
            }
        }

        public URLHolder UH
        {
            get
            {
                return _UH;
            }
        }

        public KocReloader AKR
        {
            get
            {
                return _AKR;
            }
        }

        public SeedCollector SC
        {
            get
            {
                return _SC;
            }
        }

        public MessageLogging ML { get { return _ML; } }
        #endregion

        #region kernel data
        public Boolean DataIsValid
        {
            get
            {
                return _DataIsValid();
            }
        }

        /// <summary>automatically returns a session wide unique number</summary>
        public Int32 InternalUID
        {
            get
            {
                return _intInternalUID++;
            }
        }
        #endregion

        #region logging switches
        private bool _fLoggingEnabled = false;
        private bool _fLogTrafficMonitor = false;
        private bool _fLogPostHandler = false;

        public bool LoggingEnabled
        {
            get { return _fLoggingEnabled; }
            set { _fLoggingEnabled = value; }
        }

        public bool LogTrafficMonitor
        {
            get { return _fLogTrafficMonitor; }
            set { _fLogTrafficMonitor = value; }
        }

        public bool LogPostHandler
        {
            get { return _fLogPostHandler; }
            set { _fLogPostHandler = value; }
        }

        #endregion
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
